package com.cindata

import com.cindata.dictionary.*
import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class MortgageLitigationRecordController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond MortgageLitigationRecord.list(params), model:[mortgageLitigationRecordCount: MortgageLitigationRecord.count()]
    }

    def show(MortgageLitigationRecord record)
    {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri

        def daichangShow = true
        def lianShow = true
        def kaitingShow = true
        def panjueShow = true
        def shangsu2Show = true
        def kaiting2Show = true
        def panjue2Show = true
        def zhixingShow = true
        def paimaiShow = true
        def stage = 10
        def page = "show"
        def state = record?.riskWarning?.stage?.name
        if(!state){
            page = "showBatch"
            state= record?.abnormalMortgageBatch?.stage?.name
        }
        if(state == "行长已审批诉讼" || state == "已申请诉讼" || state == "待诉讼"){
            daichangShow = false
            stage = 1
        }else if(state == "已代偿"){
            lianShow = false
            stage = 2
        }else if(state == "已立案"){
            kaitingShow = false
            stage = 3
        }else if(state == "已开庭"){
            panjueShow = false
            stage = 4
        }else if(state == "已判决"){
            shangsu2Show = false
            stage = 5
        }else if(state == "已二审上诉"){
            kaiting2Show = false
            stage = 6
        }else if(state == "已二审开庭"){
            panjue2Show = false
            stage = 7
        }else  if(state == "已二审判决"){
            zhixingShow = false
            stage = 8
        }else if(state == "已申请执行"){
            paimaiShow = false
            stage = 9
        }
        respond record, model: [record:record,lianShow: lianShow,daichangShow:daichangShow,kaitingShow:kaitingShow,panjueShow:panjueShow,shangsu2Show:shangsu2Show,kaiting2Show:kaiting2Show,panjue2Show:panjue2Show,
                                panjue2Show:panjue2Show,zhixingShow:zhixingShow,paimaiShow:paimaiShow,stage:stage,state:state],view:page
    }

    def create() 
    {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        def record = MortgageLitigationRecord.findByMortgageAndRiskWarning(Mortgage.findById(params['mortgage']),RiskWarning.findById(params['riskWarning']))
        if(record){
            respond record, model: [record:record,riskWarning: record?.riskWarning, mortgage: record?.mortgage]
        }else{
            respond true, model: [riskWarning: RiskWarning.findById(params['riskWarning']), mortgage: Mortgage.findById(params['mortgage'])]
        }
    }

    def createBatch()
    {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        def record = MortgageLitigationRecord.findByAbnormalMortgageBatch(AbnormalMortgageBatch.findById(Long.parseLong(params['abnormalMortgageBatch'])))
        if(record){
            respond record, model: [record:record,abnormalMortgageBatch: record?.abnormalMortgageBatch]
        }else{
            respond true, model: [abnormalMortgageBatch: AbnormalMortgageBatch.findById(params['abnormalMortgageBatch'])]
        }
    }

    def deferment(RiskWarning riskWarning)
    {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        respond riskWarning
    }

    def saveDeferment(RiskWarning riskWarning)
    {
        riskWarning.save(flush:true)
        redirect url: params['targetUri']
    }

    def litigation(MortgageLitigationRecord record)
    {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri

        def daichangShow = true
        def lianShow = true
        def kaitingShow = true
        def panjueShow = true
        def shangsu2Show = true
        def kaiting2Show = true
        def panjue2Show = true
        def zhixingShow = true
        def paimaiShow = true
        def stage = 10
        def page = "litigation"
        def state = record?.riskWarning?.stage?.name
        if(!state){
            page = "litigationBatch"
            state= record?.abnormalMortgageBatch?.stage?.name
        }
        if(state == "行长已审批诉讼" || state == "已申请诉讼" || state == "待诉讼"){
            daichangShow = false
            stage = 1
        }else if(state == "已代偿"){
            lianShow = false
            stage = 2
        }else if(state == "已立案"){
            kaitingShow = false
            stage = 3
        }else if(state == "已开庭"){
            panjueShow = false
            stage = 4
        }else if(state == "已判决"){
            shangsu2Show = false
            stage = 5
        }else if(state == "已二审上诉"){
            kaiting2Show = false
            stage = 6
        }else if(state == "已二审开庭"){
            panjue2Show = false
            stage = 7
        }else  if(state == "已二审判决"){
            zhixingShow = false
            stage = 8
        }else if(state == "已申请执行"){
            paimaiShow = false
            stage = 9
        }
        respond record, model: [record:record,lianShow: lianShow,daichangShow:daichangShow,kaitingShow:kaitingShow,panjueShow:panjueShow,shangsu2Show:shangsu2Show,kaiting2Show:kaiting2Show,panjue2Show:panjue2Show,
                                panjue2Show:panjue2Show,zhixingShow:zhixingShow,paimaiShow:paimaiShow,stage:stage,state:state],view:page

    }

    def saveRecord( MortgageLitigationRecord record)
    {
        if(record?.lawAccount ){
            //存在旧数据
            if(params['lawAccount1']){
                def temp = BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch,Account.findById(Long.parseLong(params['lawAccount1'])))
                temp.delete(flush:true)
            }
            def temp =  BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch,record?.lawAccount)
            if(!temp){
                BatchAccount batchAccount = new BatchAccount()
                batchAccount.abnormalMortgageBatch = record.abnormalMortgageBatch
                batchAccount.account = record.lawAccount
                batchAccount.save(flush:true)
            }
        }

        if(record?.auctioneerAccount ){
            if(params['auctioneerAccount1']){
                def temp = BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch,Account.findById(Long.parseLong(params['auctioneerAccount1'])))
                temp.delete(flush:true)
            }
            def temp = BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch, record?.auctioneerAccount)
            if (!temp) {
                BatchAccount batchAccount = new BatchAccount()
                batchAccount.abnormalMortgageBatch = record.abnormalMortgageBatch
                batchAccount.account = record.auctioneerAccount
                batchAccount.save(flush: true)
            }
        }
        record.save(flush:true)
        redirect url: params['targetUri']
    }

    @Transactional
    def save(MortgageLitigationRecord record) 
    {
        if(record?.lawAccount ){
            //存在旧数据
            if(params['lawAccount1']){
                def temp = BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch,Account.findById(Long.parseLong(params['lawAccount1'])))
                temp.delete(flush:true)
            }
                def temp =  BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch,record?.lawAccount)
                if(!temp){
                    BatchAccount batchAccount = new BatchAccount()
                    batchAccount.abnormalMortgageBatch = record.abnormalMortgageBatch
                    batchAccount.account = record.lawAccount
                    batchAccount.save(flush:true)
                }
        }

        if(record?.auctioneerAccount ){
            if(params['auctioneerAccount1']){
                def temp = BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch,Account.findById(Long.parseLong(params['auctioneerAccount1'])))
                temp.delete(flush:true)
            }
                def temp = BatchAccount.findByAbnormalMortgageBatchAndAccount(record?.abnormalMortgageBatch, record?.auctioneerAccount)
                if (!temp) {
                    BatchAccount batchAccount = new BatchAccount()
                    batchAccount.abnormalMortgageBatch = record.abnormalMortgageBatch
                    batchAccount.account = record.auctioneerAccount
                    batchAccount.save(flush: true)
                }
        }
        record.save(flush:true)
        redirect url: params['targetUri']
    }

    def edit(MortgageLitigationRecord mortgageLitigationRecord) {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        respond mortgageLitigationRecord
    }

    @Transactional
    def update(MortgageLitigationRecord mortgageLitigationRecord) {
        if (mortgageLitigationRecord == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (mortgageLitigationRecord.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond mortgageLitigationRecord.errors, view:'edit'
            return
        }

        mortgageLitigationRecord.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'mortgageLitigationRecord.label', default: 'MortgageLitigationRecord'), mortgageLitigationRecord.id])
                redirect url: params['targetUri']
            }
            '*'{ respond mortgageLitigationRecord, [status: OK] }
        }
    }

    @Transactional
    def delete(MortgageLitigationRecord mortgageLitigationRecord) {

        if (mortgageLitigationRecord == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        mortgageLitigationRecord.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'mortgageLitigationRecord.label', default: 'MortgageLitigationRecord'), mortgageLitigationRecord.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'mortgageLitigationRecord.label', default: 'MortgageLitigationRecord'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
